package cn.yuanqiao.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.system.mapper.TenantLogininforMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.yuanqiao.system.domain.SysLogininfor;
import cn.yuanqiao.system.mapper.SysLogininforMapper;
import cn.yuanqiao.system.service.ISysLogininforService;

/**
 * 系统访问日志情况信息 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysLogininforServiceImpl implements ISysLogininforService
{

    @Autowired
    private SysLogininforMapper logininforMapper;
    @Autowired
    private TenantLogininforMapper tenantLogininforMapper;

    private Map<String,Boolean> logMap = new HashMap<>();

    /**
     * 新增系统登录日志
     *
     * @param logininfor 访问日志对象
     */
    @Override
    public int insertLogininfor(SysLogininfor logininfor)
    {
        String tenantCode = logininfor.getTenantCode();
        logininfor.setInfoId(SnowflakeIdGenerator.generateId());
        if(StringUtils.isNotEmpty(tenantCode)){
            //验证表是否存在
            tenantCode = tenantCode.toUpperCase();
            if(logMap.get(tenantCode) == null){
                int num = tenantLogininforMapper.isExistTable(tenantCode+"_LOGININFOR");
                if(num == 0){
                    tenantLogininforMapper.createTenantLogininfor(tenantCode);
                }
                logMap.put(tenantCode,true);
            }
            if(logininfor.getLogLevel() == null){
                logininfor.setLogLevel(Integer.valueOf(logininfor.getStatus()));

            }
            tenantLogininforMapper.insertLogininfor(logininfor);
        }
        return logininforMapper.insertLogininfor(logininfor);
    }

    /**
     * 查询系统登录日志集合
     *
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLogininfor> selectLogininforList(SysLogininfor logininfor)
    {
        return logininforMapper.selectLogininforList(logininfor);
    }

    /**
     * 批量删除系统登录日志
     *
     * @param infoIds 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteLogininforByIds(Long[] infoIds)
    {
        return logininforMapper.deleteLogininforByIds(infoIds);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor()
    {
        logininforMapper.cleanLogininfor();
    }

    @Override
    public List<Long[]> loginTongji(SysLogininfor logininfor) {
        Map<String,Object> map=logininforMapper.loginTongji(logininfor);;
        List list=new ArrayList(6);
        for (Map.Entry<String,Object> entry : map.entrySet()) {
            Long [] value=new Long[3];
            value[0]=Long.valueOf(String.valueOf(entry.getKey().charAt(0)));
            value[1]=Long.valueOf(String.valueOf(entry.getKey().charAt(1)));
            value[2]=Long.valueOf(String.valueOf(entry.getValue()));
            list.add(value);
        }
        return list;
    }

}
